<template>
  <div style="display: inline-block">
    <el-dialog :visible.sync="dialog" :close-on-click-modal="false" :before-close="cancel" :title="title" append-to-body width="500px" @close="cancel">
      <el-form ref="form" :model="form" :rules="rules" label-width="88px">
        <el-form-item :label="$t('oldPassword')" prop="oldpassword">
          <el-input v-model="form.oldpassword" type="password" auto-complete="on" style="width: 370px;" />
        </el-form-item>
        <el-form-item :label="$t('newPassword')" prop="newpassword">
          <el-input v-model="form.newpassword" type="password" auto-complete="on" style="width: 370px;" />
        </el-form-item>
        <el-form-item :label="$t('confirmPassword')" prop="confirmpassword">
          <el-input v-model="form.confirmpassword" type="password" auto-complete="on" style="width: 370px;" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button icon="el-icon-close" @click="cancel">{{ $t('cancel') }}</el-button>
        <el-button :loading="loading" icon="el-icon-receiving" type="primary" @click="doSubmit">{{ $t('save') }}</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import store from '@/store'
import { mapGetters } from 'vuex'
import { updatePass } from '@/api/system/user'
export default {
  data() {
    const confirmPass = (rule, value, callback) => {
      if (value) {
        if (this.form.newpassword !== value) {
          callback(new Error(this.$t('passwordNotSame')))
        } else {
          callback()
        }
      } else {
        callback(new Error(this.$t('passwordAgain')))
      }
    }
    return {
      loading: false, dialog: false, title: this.$t('modifyPassword'), form: { id: '', oldpassword: '', newpassword: '', confirmpassword: '' },
      rules: {
        oldpassword: [
          { required: true, message: this.$t('oldPasswordRequired'), trigger: 'blur' }
        ],
        newpassword: [
          { required: true, message: this.$t('newPasswordRequired'), trigger: 'blur' },
          { min: 6, max: 20, message: this.$t('fieldLengthError2'), trigger: 'blur' }
        ],
        confirmpassword: [
          { required: true, validator: confirmPass, trigger: 'blur' }
        ]
      }
    }
  },
  computed: {
    ...mapGetters([
      'user'
    ])
  },
  methods: {
    cancel() {
      this.resetForm()
    },
    doSubmit() {
      this.$refs['form'].validate((valid) => {
        if (valid) {
          this.loading = true
          updatePass(
            { id: this.user.id,
              oldpassword: this.form.oldpassword,
              newpassword: this.form.newpassword }).then(res => {
            if (res.code === 0) {
              this.$notify({
                title: this.$t(res.message),
                type: 'error',
                duration: 1500
              })
              this.loading = false
            } else {
              this.$confirm('是否马上退出登录?', '提示', {
                confirmButtonText: '确定',
                cancelButtonText: '取消',
                type: 'warning'
              }).then(() => {
                this.loading = false
                this.resetForm()
                store.dispatch('LogOut').then(() => {
                  location.reload() // 为了重新实例化vue-router对象 避免bug
                })
              }).catch(() => {
                this.loading = false
              })
            }
          }).catch(err => {
            console.log(err)
          })
        } else {
          return false
        }
      })
    },
    resetForm() {
      this.dialog = false
      this.$refs['form'].resetFields()
      this.form = { oldpassword: '', newpassword: '', confirmpassword: '' }
    }
  }
}
</script>

<style scoped>

</style>
